Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid closing over values in string eval when comparing values #1018

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

haarg
Copy link
Member

@haarg haarg commented Jan 14, 2025

When comparing values using Test::Builder::cmp_ok, a string eval is used to match the caller location, and to inject an arbitrary comparison op. When used with Devel::Cover, the string eval may close over any variables used in it, and maintain them for the lifetime of the program.

Instead of comparing the values directly in the string eval, generate an anonymous sub that will perform the comparison, and call it in a block eval.

This is a reoccurrance of a previous problem that was fixed with 4fe707c. Some combination of perl or module updates has brought back the issue. While there was a test included with the bug, it only fails when run with Devel::Cover, which isn't done during the automated or release testing.

@haarg
Copy link
Member Author

haarg commented Jan 14, 2025

The test script for this could be patched to try running with Devel::Cover, but it's rather ugly. Here's a patch that adds this:

diff --git i/t/Legacy/Regression/is_capture.t w/t/Legacy/Regression/is_capture.t
index 1b8c73e10..b36f8d264 100644
--- i/t/Legacy/Regression/is_capture.t
+++ w/t/Legacy/Regression/is_capture.t
@@ -1,3 +1,31 @@
+BEGIN {
+    if ($ENV{RELEASE_TESTING}) {
+        if (!$INC{'Devel/Cover.pm'}) {
+            delete $ENV{DEVEL_COVER_OPTIONS};
+            require File::Temp;
+            my $temp = File::Temp::tempdir(
+                'Test-Simple-XXXXXX',
+                TMPDIR  => 1,
+                CLEANUP => 1,
+            );
+            require Devel::Cover;
+            Devel::Cover->import(
+                -ignore   => '.',
+                -summary  => 0,
+                -silent   => 1,
+                -coverage => 'none',
+                -blib     => 0,
+                -dir      => $temp,
+                -db       => $temp,
+            );
+        }
+    }
+    else {
+        require Test2::Tools::Tiny;
+        Test2::Tools::Tiny::skip_all('Only running under RELEASE_TESTING');
+        exit;
+    }
+}
 use strict;
 use warnings;
 use Test2::Tools::Tiny;

I can add this to the PR if desired. It would also need to install Devel::Cover during the automated testing, which has its own complications.

@exodist
Copy link
Member

exodist commented Jan 16, 2025

I agree the test is ugly, and we can skip it. However I would like a comment added to the patch explaining why we do the sub wrapping, that will help avoid repeating the bug later by any future contributors.

When comparing values using Test::Builder::cmp_ok, a string eval is
used to match the caller location, and to inject an arbitrary comparison
op. When used with Devel::Cover, the string eval may close over any
variables used in it, and maintain them for the lifetime of the program.

Instead of comparing the values directly in the string eval, generate an
anonymous sub that will perform the comparison, and call it in a block
eval.

This is a reoccurrance of a previous problem that was fixed with
4fe707c. Some combination of perl or
module updates has brought back the issue. While there was a test
included with the bug, it only fails when run with Devel::Cover, which
isn't done during the automated or release testing.
@haarg haarg force-pushed the no-capture-cmp_ok branch from cc69866 to a49f733 Compare January 18, 2025 14:50
@haarg
Copy link
Member Author

haarg commented Jan 18, 2025

Updated to include a comment.

@exodist exodist merged commit 6e4323d into Test-More:master Jan 21, 2025
19 checks passed
exodist added a commit that referenced this pull request Jan 21, 2025
    - avoid closing over values in string eval when comparing values #1018 (Thanks Haarg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants